package com.hh.front.controller;

import com.hh.common.page.CommonPage;
import com.hh.common.request.CreateOrderRequest;
import com.hh.common.request.NewPreOrderRequest;
import com.hh.common.request.OrderRefundApplyRequest;
import com.hh.common.request.PageParamRequest;
import com.hh.common.response.*;
import com.hh.service.service.NewOrderService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController()
@RequestMapping("api/front/order")
public class OrderController {

    @Autowired
    private NewOrderService orderService;


    /**
     * 预下单
     * @param request
     * @return
     */
    @RequestMapping(value = "/preOrder", method = RequestMethod.POST)
    public CommonResult<Map<String, Object>> preOrder(@RequestBody @Validated NewPreOrderRequest request) {
        return CommonResult.success(orderService.preOrder(request));
    }

    /**
     * 加载预下单
     * @param preOrderNo
     * @return
     */
    @RequestMapping(value = "loadPreOrder/{preOrderNo}", method = RequestMethod.GET)
    public CommonResult<PreOrderResponse> loadPreOrder(@PathVariable String preOrderNo) {
        return CommonResult.success(orderService.loadPreOrder(preOrderNo));
    }

    /**
     * 创建订单
     * @param orderRequest
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<Map<String, Object>> createOrder(@Validated @RequestBody CreateOrderRequest orderRequest) {
        return CommonResult.success(orderService.createOrder(orderRequest));
    }



    /**
     * 订单列表
     * @param type 类型
     * @param pageRequest 分页
     * @return 订单列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "评价等级|0=未支付,1=待发货,2=待收货,3=待评价,4=已完成,-3=售后/退款", required = true)
    })
    public CommonResult<CommonPage<NewOrderDetailResponse>> orderList(@RequestParam(name = "type") Integer type,String keyword,
                                                                      @ModelAttribute PageParamRequest pageRequest) {
        return CommonResult.success(orderService.list(type, keyword, pageRequest));
    }

    /**
     * 订单详情
     * @param orderId 订单编号
     * @return 订单详情
     */
    @ApiOperation(value = "订单详情")
    @RequestMapping(value = "/detail/{orderId}", method = RequestMethod.GET)
    public CommonResult<NewStoreOrderDetailInfoResponse> orderDetail(@PathVariable String orderId) {
        return CommonResult.success(orderService.detailOrder(orderId));
    }


    /**
     * 订单取消
     * @param orderId String 订单编号
     */
    @ApiOperation(value = "订单取消")
    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    public CommonResult<Boolean> cancel(@RequestParam(value = "orderId") String orderId) {
        if(orderService.cancel(orderId)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 订单退款申请
     * @param request OrderRefundApplyRequest 订单id
     */
    @ApiOperation(value = "订单退款申请")
    @RequestMapping(value = "/refund", method = RequestMethod.POST)
    public CommonResult<Boolean> refundApply(@RequestBody @Validated OrderRefundApplyRequest request) {
        if(orderService.refundApply(request)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 删除订单
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public CommonResult<Object> del(@RequestParam(value = "orderId") String orderId) {
        if(orderService.del(orderId)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed("删除失败，订单取消后才能删除");
        }
    }

    /**
     * 提醒发货
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/remindSend", method = RequestMethod.POST)
    public CommonResult<Boolean> remindSend(@RequestParam(value = "orderId") String orderId) {
        return CommonResult.success("提醒成功");
    }


    /**
     * 订单收货
     * @param orderId Integer 订单id
     */
    @ApiOperation(value = "订单收货")
    @RequestMapping(value = "/take", method = RequestMethod.POST)
    public CommonResult<Boolean> take(@RequestParam(value = "orderId") String orderId) {
        if(orderService.take(orderId)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }


    /**
     * 查看退款原因
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/showRefundInfo", method = RequestMethod.GET)
    public CommonResult<String> showRefundInfo(@RequestParam(value = "orderId") String orderId) {
        return CommonResult.success(orderService.showRefundInfo(orderId),"ok");
    }


}
